package jUnit;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Iterator;

import org.junit.Test;

import Harmonisation.Accord;
import Harmonisation.Chant;
import Harmonisation.Harmonisation;
import Harmonisation.Note;
import Harmonisation.CreationDuChant.GrapheAccords;
import Harmonisation.Voix.Basse;

public class TestNote {

	@Test
	public void creationNote() {
		Note n = new Note("do3", 1);
		if (!n.getNom().equals("do")) {
			fail("Mauvais nom.");
		}
		if (n.getOctave() != 3) {
			fail("Mauvaise octave.");
		}
		if (n.getNote() != 14) {
			fail("Mauvaise valeur note.");
		}
		if (n.getMidi() != 48) {
			fail("Mauvais valeur midi.");
		}

		Note n2 = new Note("sol1", 1);
		if (!n2.getNom().equals("sol")) {
			fail("Mauvais nom.");
		}
		if (n2.getOctave() != 1) {
			fail("Mauvaise octave.");
		}
		if (n2.getNote() != 4) {
			fail("Mauvaise valeur note.");
		}
		if (n2.getMidi() != 31) {
			fail("Mauvais valeur midi.");
		}
	}
	

	@Test
	public void generationDesEnchainementsDAccords()
	{
	ArrayList<Note> notes = new ArrayList<Note>();
		
		notes.add(new Note("do4", 1));
		notes.add(new Note("do4", 1));
		notes.add(new Note("re4", 1));
		notes.add(new Note("mi4", 1));
		notes.add(new Note("do4", 1));
		notes.add(new Note("sol4", 1));
		notes.add(new Note("fa4", 1));	
		notes.add(new Note("fa4", 1));	
		notes.add(new Note("do4", 1));	
		notes.add(new Note("do4", 1));	
		notes.add(new Note("fa4", 1));	
		
		
		Harmonisation test = new Harmonisation(notes);
		
		ArrayList<Accord> enchainementCourant;
		
		Iterator<Note> itNote = test.generationChantAleatoire().getBasse().getPartition().iterator();
		ArrayList<Accord> arrayAccord = GrapheAccords.getListAccord();

		Iterator<ArrayList<Accord>> itAccord =  test.getGrapheAccord().getAccordPossibleSopranoAChaqueTemps(notes).iterator();
		ArrayList<Accord> accordPossibleAUnTemps; 
		Accord accord = null, accordPrecedent =null;
		
		while(itNote.hasNext() && itAccord.hasNext())
		{
			accordPossibleAUnTemps = itAccord.next();
			Note n = itNote.next();
			
			Iterator<Accord> itVerifAccord = arrayAccord.iterator();
			while(itVerifAccord.hasNext())
			{
				Accord verifAcc = itVerifAccord.next();
				if(verifAcc.estTonique(n))
				{
					if(! accordPossibleAUnTemps.contains(verifAcc) && !verifAcc.equals(new Accord("fa", "la", "do", "ivb")))
					{
						System.out.println(accordPossibleAUnTemps+" ne contient pas "+verifAcc);
						fail("L'accord n'est pas possible a ce temps la.");
					}
				}
			}
			if(accordPrecedent != null)
			{
				if (! accordPrecedent.EstVoisin(accord)) {
					fail("Mauvais enchainement.");
				}
			}
			accordPrecedent = accord;
		}
		

	}


	

}
